<?php
if ( defined('ABSPATH') )
	include_once ABSPATH . 'config.php';
else
    include_once '../config.php';

include_once("curl.php");


class Signature
{
	var $id;
	var $ownerid;
	var $address;
	var $signature;
	var $expires;
	
	function Signature($ownerid, $address, $url='', $expires='10')
	{
		$this->ownerid = $ownerid;
		$this->address = $address;
		if(strpos($url,"?") != false)
		{
			$urlArr = explode("?",$url);
			$parmArr = explode("&",$urlArr[1]);
			$keyArr = array();
			$valueArr = array();
			foreach($parmArr as $parm)
			{
				$parmArr = explode("=",$parm);
				array_push($keyArr,$parmArr[0]);
				$valueArr[$parmArr[0]] = $parmArr[1];
				
			}
			sort($keyArr);
			foreach($keyArr as $key)
			{
				$q .= $key.$valueArr[$key];
			}
			$this->signature = md5($q);
			//$this->signature = $q;
			
			$this->expires =  strtotime("+$expires minutes", time());
			
		}
	}
	
	function Get()
	{
		global $db;
		
		$rs = $db->get_results("select ownerid, address, signature, expires from $db->signature where ownerid='$this->ownerid' and address='$this->address' and expires>now()", ARRAY_A);
		
		return $rs;
	
	}
	
	function Save()
	{
		global $db;
		
		$db->query("insert into $db->signature (ownerid, address, signature, expires) values('$this->ownerid','".$db->escape($this->address)."','".$db->escape($this->signature)."','".date('Y-m-d H:i:s',$this->expires)."')");
		
		//Cleanup -- Delete expired signatures.
		$db->query("delete from $db->signature where expires < now()");
		
	}
	
	

	
	
	
	
	
	
}













?>